package cn.ds.assets.module.assets.controller.admin.metadataJobInstance;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.metadataJobInstance.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataJobInstance.BaseMetadataJobInstanceDO;
import cn.ds.assets.module.assets.convert.metadataJobInstance.BaseMetadataJobInstanceConvert;
import cn.ds.assets.module.assets.service.metadataJobInstance.BaseMetadataJobInstanceService;

//@Tag(name = "管理后台 - 元数据管理-元数据采集作业实例")
//@RestController
//@RequestMapping("/assets/base-metadata-job-instance")
@Validated
public class BaseMetadataJobInstanceController {

    @Resource
    private BaseMetadataJobInstanceService baseMetadataJobInstanceService;

    @PostMapping("/create")
    @Operation(summary = "创建元数据管理-元数据采集作业实例")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:create')")
    public CommonResult<Long> createBaseMetadataJobInstance(@Valid @RequestBody BaseMetadataJobInstanceCreateReqVO createReqVO) {
        return success(baseMetadataJobInstanceService.createBaseMetadataJobInstance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新元数据管理-元数据采集作业实例")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:update')")
    public CommonResult<Boolean> updateBaseMetadataJobInstance(@Valid @RequestBody BaseMetadataJobInstanceUpdateReqVO updateReqVO) {
        baseMetadataJobInstanceService.updateBaseMetadataJobInstance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除元数据管理-元数据采集作业实例")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:delete')")
    public CommonResult<Boolean> deleteBaseMetadataJobInstance(@RequestParam("id") Long id) {
        baseMetadataJobInstanceService.deleteBaseMetadataJobInstance(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得元数据管理-元数据采集作业实例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:query')")
    public CommonResult<BaseMetadataJobInstanceRespVO> getBaseMetadataJobInstance(@RequestParam("id") Long id) {
        BaseMetadataJobInstanceDO baseMetadataJobInstance = baseMetadataJobInstanceService.getBaseMetadataJobInstance(id);
        return success(BaseMetadataJobInstanceConvert.INSTANCE.convert(baseMetadataJobInstance));
    }

    @GetMapping("/list")
    @Operation(summary = "获得元数据管理-元数据采集作业实例列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:query')")
    public CommonResult<List<BaseMetadataJobInstanceRespVO>> getBaseMetadataJobInstanceList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseMetadataJobInstanceDO> list = baseMetadataJobInstanceService.getBaseMetadataJobInstanceList(ids);
        return success(BaseMetadataJobInstanceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得元数据管理-元数据采集作业实例分页")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:query')")
    public CommonResult<PageResult<BaseMetadataJobInstanceRespVO>> getBaseMetadataJobInstancePage(@Valid BaseMetadataJobInstancePageReqVO pageVO) {
        PageResult<BaseMetadataJobInstanceDO> pageResult = baseMetadataJobInstanceService.getBaseMetadataJobInstancePage(pageVO);
        return success(BaseMetadataJobInstanceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出元数据管理-元数据采集作业实例 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-job-instance:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseMetadataJobInstanceExcel(@Valid BaseMetadataJobInstanceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseMetadataJobInstanceDO> list = baseMetadataJobInstanceService.getBaseMetadataJobInstanceList(exportReqVO);
        // 导出 Excel
        List<BaseMetadataJobInstanceExcelVO> datas = BaseMetadataJobInstanceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "元数据管理-元数据采集作业实例.xls", "数据", BaseMetadataJobInstanceExcelVO.class, datas);
    }

}
